package tree.treerecursion;


import tree.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @Classname : MaximumDepthOfBinaryTree
 * @Description :
 * <a href="https://leetcode.cn/problems/maximum-depth-of-binary-tree/">104. 二叉树的最大深度</a>
 * @Author : chentianyu
 * @Date 2022/12/30 20:43
 */


public class MaximumDepthOfBinaryTree {

    /**
     * 广度优先搜索
     *
     * @param root
     * @return
     */
    public int maxDepth2(TreeNode root) {
        if (root == null) return 0;

        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        int maxDepth = 0;
        while (!q.isEmpty()) {
            int size = q.size();
            for (int i = 0; i < size; i++) {
                TreeNode cur = q.poll();
                if (cur.left != null) q.offer(cur.left);
                if (cur.right != null) q.offer(cur.right);
            }
            maxDepth++;
        }
        return maxDepth;
    }


    /**
     * 深度优先搜索（递归法）
     *
     * @param root
     * @return
     */
    public int maxDepth(TreeNode root) {
        if (root == null) return 0;

        return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
    }
}
